1.如何在LinearLayout中,使EditText充满整行
使用“android:layout_weight”这个属性,调整该标签的占比,比如下面的这个代码,在EditText中添加该属性等于“1”,则EditText将TextView剩下的部分填满,如果在TextView中也添加该代码,则两个组件按照1:1的比例进行布局。系统会自动将一行或者一列中的weight属性值相加,每一项的weight除这个值则为该组件再该行或列中的占比。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"/>
<EditText
android:id="@+id/editText"
android:hint="@string/text_editText"
android:layout_weight="1"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
2.使用LinearLayout或者RelativeLayout时多个组件重叠在一起
LinearLayout中,在属性中添加“android:orientation”,vertical为垂直排列,horizontal为水平排列。
RelativeLayout中需要添加相对于某一组件的位置,不如位于xx组件下方等。
3.RecycleView无法导入
在Android Studio左侧点击该项目,按F4打开Project Structure,点击Dependencies标签后,点击右侧的加号,然后点击Library dependency,然后找到recycleview添加即可。
4.R.xx中的R变红报错
我出现过好几次这种情况,有的时候可能是Android Studio的问题,重启一下软件、Rebuild一下或者新建一个工程将代码复制一遍都可能解决问题。如果用了这些办法后问题没有解决,基本就是XML文件写的有问题。比如我在写计算器的时候,用了上边的方法后就没有解决问题,R还是红色的,错误提示告诉我实在科学计算器的界面出现了错误,找到出错的一行,经过研究发现,有些符号得添加转义字符才能使用,添加转义字符后错误就解决了。
5.无法隐藏标题栏
许多软件没有最上方的标题栏,通过搜索发现可以通过在MainActivity中添加
“requestWindowFeature(Window.FEATURE_NO_TITLE);”
即可,且该语句一定要写在“setContentView()”方法前。但是我改完代码后发现不能隐藏,通过查阅资料,发现继承AppCompatActivity时无法隐藏,将继承的类改为Activity即可。
6.何时继承AppCompatActivity类何时继承Activity类
通过查阅资料在国外网站找到了下面的答案:
Activity is the baseline. Every activity inherits from Activity, directly or indirectly.
FragmentActivity is for use with the backport of fragments found in the support-v4 and support-v13 libraries. The native implementation of fragments was added in API Level 11, which is higher than your proposed minSdkVersion values. The only reason why you would need to consider FragmentActivity specifically is if you want to use nested fragments (a fragment holding another fragment), as that was not supported in native fragments until API Level 17.
AppCompatActivity is from the appcompat-v7 library. Principally, this offers a backport of the action bar. Since the native action bar was added in API Level 11, you do not need AppCompatActivity for that. However, current versions of appcompat-v7 also add a limited backport of the Material Design aesthetic, in terms of the action bar and various widgets. There are pros and cons of using appcompat-v7, well beyond the scope of this specific Stack Overflow answer.
ActionBarActivity is the old name of the base activity from appcompat-v7. For various reasons, they wanted to change the name. Unless some third-party library you are using insists upon an ActionBarActivity, you should prefer AppCompatActivity over ActionBarActivity.
同时该回答者还做了总结:
1.If you want the backported Material Design look, use AppCompatActivity
2.If not, but you want nested fragments, use FragmentActivity
3.If not, use Activity
通过该总结则可看出什么时候继承什么类了。
7.通过Android Studio无法上传代码
上传代码时弹出这个错误
Can't finish GitHub sharing processSuccessfully created project 'Test' on GitHub, but initial commit failed:
* Please tell me who you are. Run git config --global user.email "[email protected]" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: empty ident name (for (null)>) not allowed during executing git -c core.quotepath=false commit -m "Initial commit" --
通过搜索该问题后,发现原来是git没有配置的原因,运行Git Bash,输入下面两行代码即可:
`git config --global user.email "[email protected]"
git config --global user.name "Your Name"`
其中邮箱和姓名填写自己的。
8.无法在Android Device Monitor中打开data文件夹查看数据
因为我的电脑是AMD显卡,无法打开Android自带的虚拟机,所以我采用真机测试的方法。但是真机测试因为权限的问题,为了保证安全性,即使通过添加代码给了权限,也没法打开。
9.Button按钮中大小写问题
Button中的android:text属性中复制后,在显示的时候,所有的字母自动变成了大写,这是设置属性android:textAllCaps="false"即可。
10.创建项目时选错版本,手机无法测试,如何不重建降低版本
首先在该项目文件下点击F4,然后选择该Modules,点击右侧的Flavors,在Min sdk version 中,点击需要的最小等级即可。
11.如何为ListView添加监听器
通过get(position)方法。
修改代码为:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
DataAdapter adapter = new DataAdapter(MainActivity.this,R.layout.data_item,list);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
ListData data = list.get(position);
Toast.makeText(MainActivity.this, data.getName(), Toast.LENGTH_SHORT).show();
}
});
}
这样就通过get方法得到点击的哪一项,然后再进行操作就可以了。
12.计算器中如何处理优先级
要处理复杂的表达式,用户先将表达式输入完,然后进行处理,所以我选择用堆栈进行处理,分数字栈和符号栈,然后根据符号的优先级,进行出栈和压栈处理,进行计算。但是对于符号的优先级,我只知道加减乘除的,但是对于Sin等三角函数,还有开方运算的优先级是不知道的。所以先将表达式进行处理。通过循环,不断对表达式中的符号进行进行处理,最终只剩加减乘除。对于处理,以Sin为例。
if (formula.contains("Sin")) {
String temp = formula.split("Sin\\(")[1];
int temp2 = 0;
for (int i = 0; i < temp.length(); i++) {
if(tempRight.charAt(symbolCount) == '('){
leftFish ++;
}
if (temp.charAt(i) == ')' && leftFish ==0) {
temp2 = i;
break;
}
if(temp.charAt(i) == ')'){
leftFish--;
}
}
leftFish = 0;
temp = temp.substring(0, temp2);
Calculate tempCal = new Calculate(temp);
temp = tempCal.result;
symbolvalue = Math.sin(Double.parseDouble(tempCal.result));
tempLeft = formula.split("Sin\\(")[0];
for (symbolCount = 0; symbolCount < formula.length(); symbolCount++) {
if (formula.charAt(symbolCount) == 'S') {
symbolCount = symbolCount + 1;
for (; symbolCount < formula.length(); symbolCount++) {
if (formula.charAt(symbolCount) == '(') {
symbolCount++;
break;
}
}
break;
}
}
tempRight = formula.substring(symbolCount);
for (symbolCount = 0; symbolCount < tempRight.length(); symbolCount++) {
if(tempRight.charAt(symbolCount) == '('){
leftFish++;
}
if (tempRight.charAt(symbolCount) == ')' && leftFish ==0) {
break;
}
if(tempRight.charAt(symbolCount) == ')'){
leftFish--;
}
}
leftFish = 0;
if (symbolCount == tempRight.length()) {
tempRight = "";
} else {
tempRight = tempRight.substring(symbolCount + 1);
}
if (symbolvalue.toString().charAt(0) == '-') {
String tempValue = "";
for (int k = 1; k < symbolvalue.toString().length(); k++) {
tempValue += symbolvalue.toString().charAt(k);
}
symbolvalue = Double.parseDouble(tempValue);
formula = tempLeft + "0-" + symbolvalue + tempRight;
} else {
formula = tempLeft + symbolvalue + tempRight;
}
Log.v("MyTagSin", formula);
continue;
}
代码的流程是,先通过spilt()方法将算式分割,分割后右边的第一部分就是Sin中的内容的开头,然后通过循环,找到对应的右括号,即Sin中内容的结束为止,因为Sin中能再进行复杂计算,所以Sin中可能还有括号,这样的话第一个右括号就不是Sin所对应的右括号,所以设置一个变量来记录这一部分左括号出现的次数,没出现一次左括号,就加一,遇到一个右括号时,先判断此时该变量是不是0,如果是,则记录该右括号位置,如果不是则该变量减一。这样就可以取得Sin中的算式了。
得到该算式后,创建Calculate对象,再进行计算,如果该算式中还有Sin等符号,则再通过循环进行刚才的操作,如果没有了Sin等符号,则通过栈计算出表达式的数值。然后电泳Math中计算Sin的方法,对刚才分理出的结果进行计算,最终的结果就是该Sin的值。
得到表达式的数值后就要进行算式的拼接还是通过spilt()方法进行分割,但是这回取得是Sin左边的部分,然后通过上一步的循环方法,再得到Sin对应的右括号位置,取得改右括号之后的部分。这就是需要拼接的左右两个部分。在两部分中间加入刚才计算出来的Sin值就可以了。
但是因为计算出来的Sin值可能是负数,这样的话表达式可能会出现“1 - - 2 + 3”的形式,是一个不规范的表达式,所以添加一步判断,若是负值,在拼接的时候,该Sin值前面加上一段“0 ”,表示用零减去这个数。然后拼接在一起就完成了。
Cos、Tan和根号采用相同的方法。
对于堆栈进算方法,代码如下:
```
if (leftBrackets != rightBrackets || error != 0) {
System.out.println("输入有误");
} else {
double numTemp = 0;
int j = 0;
while (j < formula.length()) {
//数字入栈
while (formula.charAt(j) >= 48 && formula.charAt(j) <= 57 || formula.charAt(j) == 46) {
if (formula.charAt(j) >= 48 && formula.charAt(j) <= 57) {
numTemp *= 10;
numTemp += Double.parseDouble(formula.charAt(j) + "");
}
//读取带小数的数字
else if (formula.charAt(j) == 46) {
j++;
for (int i = -1; formula.charAt(j) >= 48 && formula.charAt(j) <= 57; i--) {
numTemp += Double.parseDouble(formula.charAt(j) + "") * Math.pow(10, i);
if ((formula.charAt(j + 1) < 48 || formula.charAt(j + 1) > 57)) {
break;
} else
j++;
}
}
if (j <= formula.length() - 1)
j++;
else {
num.push(new Double(numTemp));
break;
}
if ((formula.charAt(j) < 48 || formula.charAt(j) > 57) && formula.charAt(j) != 46) {
num.push(new Double(numTemp));
numTemp = 0;
}
}
//符号入栈
if (formula.charAt(j) >= 40 && formula.charAt(j) <= 47 || formula.charAt(j) == 35) {
//符号栈空是直接入栈
if (operator.isEmpty()) {
operator.push(new Character('#'));
operator.push(new Character(formula.charAt(j)));
j++;
} else {
//右边符号优先级高
if (Right(formula.charAt(j)) > Left(operator.get().charValue())) {
operator.push(new Character(formula.charAt(j)));
j++;
}
//左边和右边符号优先级一样
else if (Right(formula.charAt(j)) == Left(operator.get().charValue())) {
operator.pop();
j++;
}
//左边符号优先级高
else
switch (operator.pop().charValue()) {
case '+':
num.push(new Double(num.pop().doubleValue() + num.pop().doubleValue()));
break;
case '-':
double x = num.pop().doubleValue();
num.push(new Double(num.pop().doubleValue() - x));
break;
case '*':
num.push(new Double(num.pop().doubleValue() * num.pop().doubleValue()));
break;
case '/':
double y = num.pop().doubleValue();
num.push(new Double(num.pop().doubleValue() / y));
break;
default:
return;
}
}
}
}
BigDecimal bg = new BigDecimal(num.get());
result = bg.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
// System.out.println(num.get());
}
}
}
//左边符号优先级
public int Left(char op) {
switch (op) {
case '+':
return 3;
case '-':
return 3;
case '*':
return 5;
case '/':
return 5;
case '(':
return 1;
case ')':
return 6;
case '#':
return 0;
default:
return op;
}
}
//右边符号优先级
public int Right(char op) {
switch (op) {
case '+':
return 2;
case '-':
return 2;
case '*':
return 4;
case '/':
return 4;
case '(':
return 6;
case ')':
return 1;
case '#':
return 0;
default:
return op;
}
}
```
符号入栈部分的操作流程是,遇到符号后,先判断符号栈是不是为空,若为空,则将“#”和该符号入栈。若不为空则需要比较该符号和栈里的符号的优先级。
如果该符号的优先级高,则直接入栈。如果一样,则将符号栈栈顶元素出栈,相当于两个符号抵消(比如两个括号,或者两个井号)。如果栈顶的优先级高,则需要将数字栈顶端的两个元素出栈,符号栈顶端元素出栈,让两个数字经过对应的出栈符号进行计算,然后将计算结果再压入数字栈中。
不断循环直到算式处理完成。